<template>
  <div>
    <div>
      <div class="handle-box" style="display: flex">
        <el-input v-model="pagination.name" placeholder="请输入歌曲名称" style="width: 300px"></el-input>
        <el-input v-model="pagination.artist" placeholder="请输入歌手名" style="margin-left: 15px;width: 300px"></el-input>
        <el-select clearable v-model="pagination.artistType" placeholder="请选择歌手类型" class="handle-select mrb10" style="margin-left: 15px">
          <el-option key="1" label="华语男歌手" :value="0"></el-option>
          <el-option key="2" label="华语女歌手" :value="1"></el-option>
          <el-option key="3" label="中国组合" :value="2"></el-option>
          <el-option key="4" label="日韩男歌手" :value="3"></el-option>
          <el-option key="5" label="日韩女歌手" :value="4"></el-option>
          <el-option key="6" label="欧美歌手" :value="5"></el-option>
          <el-option key="7" label="外国组合" :value="6"></el-option>
        </el-select>
        <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
        <el-button type="success" @click="$router.push({path: '/musicEdit'})">新增歌曲</el-button>
      </div>
      <el-table :data="musics" border class="table" header-cell-class-name="table-header">
        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
        <el-table-column prop="name" label="歌曲名称" align="center"></el-table-column>
        <el-table-column prop="artist" label="歌手" align="center"></el-table-column>
        <el-table-column prop="artistType" label="歌手类型" align="center">
          <template slot-scope="scope">
            {{getArtistTypeText(scope.row.artistType)}}
          </template>
        </el-table-column>
        <el-table-column prop="filePath" label="存储路径" align="center"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column prop="updateTime" label="修改时间" align="center"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" style="color: var(--orangeRed)"
                       @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button type="text" icon="el-icon-delete" style="color: var(--orangeRed)"
                       @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next"
                       :current-page="pagination.current"
                       :page-size="pagination.size"
                       :total="pagination.total"
                       @current-change="handlePageChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
  export default {
    components: {},
    data() {
      return {
        pagination: {
          current: 1,
          size: 10,
          total: 0,
          name: null,
          artist: null,
          artistType: null
        },
        musics: []
      }
    },

    computed: {},

    watch: {},

    created() {
      this.getMusics();
    },

    mounted() {
    },

    methods: {
      getArtistTypeText(artistType) {
        switch (artistType) {
          case 0:
            return "华语男歌手";
          case 1:
            return "华语女歌手";
          case 2:
            return "中国组合";
          case 3:
            return "日韩男歌手";
          case 4:
            return "日韩女歌手";
          case 5:
            return "欧美歌手";
          case 6:
            return "外国组合";
          default:
            return "未知";
        }
      },
      handleDelete(item) {
        const id = item.id;
        const baseURL = this.$constant.baseURL;
        this.$confirm('确认删除音乐？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'success',
          center: true
        }).then(() => {
          this.axios({
            method: 'DELETE',
            url: `${baseURL}/play/deleteMusic/${id}`,
          }).then(res => {
            this.pagination.current = 1;
            this.getMusics();
            this.$message({
              message: "删除成功！",
              type: "success"
            });
          }).catch(error => {
            this.$message({
              message: error.message,
              type: "error"
            });
          })
        }).catch(() => {
          this.$message({
            type: 'success',
            message: '已取消删除!'
          });
        });
      },
      search() {
        this.pagination.total = 0;
        this.pagination.current = 1;
        this.getMusics();
      },
      getMusics() {
        this.$http.post(this.$constant.baseURL + "/play/musicList", this.pagination, true)
          .then((res) => {
            if (!this.$common.isEmpty(res.data)) {
              // 格式化每条记录的 createTime 字段
              this.musics = res.data.records.map(music => ({
                ...music,
                createTime: this.$formatTime(music.createTime),
                updateTime: this.$formatTime(music.updateTime)
              }));
              this.pagination.total = res.data.total;
            }
          })
          .catch((error) => {
            this.$message({
              message: error.message,
              type: "error"
            });
          });
      },
      handlePageChange(val) {
        this.pagination.current = val;
        this.getMusics();
      },
      handleEdit(row) {
        this.$router.push({path: '/musicEdit', query: {id: row.id}})
      },
    }
  }
</script>

<style scoped>

  .handle-box {
    margin-bottom: 20px;
  }

  .handle-select {
    width: 200px;
  }

  .table {
    width: 100%;
    font-size: 14px;
  }

  .mrb10 {
    margin-right: 10px;
  }

  .table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
  }

  .pagination {
    margin: 20px 0;
    text-align: right;
  }

  .el-switch {
    margin: 5px;
  }
</style>
